//
//  PersonalBiddingViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 15/12/23.
//  Copyright © 2015年 GripMeMory. All rights reserved.
//

#import "PersonalBiddingViewController.h"
#import "BigTableView.h"

#import "UIComboBox.h"
#import "JMJSortButton.h"

#define kSegementedControlTopMargin 23 / 2
#define kSegementedControlLeftMargin 20 / 2
#define kSegementedControlHeight 30

#define kSelectedViewTopMargin 10
#define kSelectedViewHeight 50

#define kSeparateLineLeftMargin 10

#define kNoticeBlockHeight 15

#define kNoticeLabelHeight 25 / 2
#define kNoticeLabelTopMargin 10 / 2
#define kNoticeLabelBottomMargin 25 /2

#define kImageViewWidth 40 / 2
#define kSortedButtonWidth 140 / 2
#define kSortedButtonRightMargin 20 / 2

#define kComboBoxTopMargin 20 / 2
#define kComboBoxLeftMargin 70 / 2
#define kFilterButtonWidth 100 / 2

@interface PersonalBiddingViewController ()

@end

@implementation PersonalBiddingViewController

{
    UISegmentedControl *_segmentedControl;
    UILabel *_displayLabel;
    UIView *_selectedView;
    UIView *_noticeView;
    BigTableView *_bigTableView;
    UILabel *_noticeLabel;
    UIComboBox *_combox;
    JMJSortButton *_sortedBtn1;
    JMJSortButton *_sortedBtn2;
    UIView *_filterView1;
    UIButton *_filterBtn;
}

#pragma mark - View Controller Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavBar];
    [self customMainView];
    [self customSelectedView];
    [self customNoticeView];
    [self customTableView];
    [self customFilterView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Custom Userinterface

- (void)customNavBar{
    
    self.title = @"个人竞价室";
    UIImage *normalImage = [[UIImage imageNamed:@"个人竞价室1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selectedImage = [[UIImage imageNamed:@"个人竞价室.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationController.tabBarItem.image = normalImage;
    self.navigationController.tabBarItem.selectedImage = selectedImage;
    
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"消息"] style:UIBarButtonItemStylePlain target:self action:@selector(bbiClick:)];
    self.navigationItem.rightBarButtonItem = bbi;
}

#pragma mark - Custom User Interface

- (void)customMainView
{
    _segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"我的报价标的", @"我关注的标的", @"我的报价记录"]];
    _segmentedControl.frame = CGRectMake(kSegementedControlLeftMargin, kNavigationBarHeight +kSegementedControlTopMargin , UISCREENWIDTH - 2 * kSegementedControlLeftMargin, kSegementedControlHeight);
    _segmentedControl.tintColor = RGB_COLOR(@"#d20714");
    _segmentedControl.selectedSegmentIndex = 0;
    [_segmentedControl addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_segmentedControl];
}

- (void)customSelectedView{
    _selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, _segmentedControl.bottom + kSelectedViewTopMargin, UISCREENWIDTH, kSelectedViewHeight)];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(_selectedView.left + kSeparateLineLeftMargin, 0, UISCREENWIDTH - 2 * kSeparateLineLeftMargin, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [_selectedView addSubview:line];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kSegementedControlLeftMargin, 0, kImageViewWidth, kSelectedViewHeight)];
    imageView.image = [UIImage imageNamed:@"时钟1"];
    imageView.contentMode = UIViewContentModeCenter;
    [_selectedView addSubview:imageView];
    
    _sortedBtn1 = [[JMJSortButton alloc] init];
    _sortedBtn1.frame = CGRectMake(imageView.right, 0, kSortedButtonWidth, kSelectedViewHeight);
    _sortedBtn1.title = @"剩余时间";
    [_selectedView addSubview:_sortedBtn1];
    
    _sortedBtn2 = [[JMJSortButton alloc] init];
    _sortedBtn2.frame = CGRectMake(_sortedBtn1.right + kSortedButtonRightMargin, 0, kSortedButtonWidth, kSelectedViewHeight);
    _sortedBtn2.title = @"报价金额";
    
    _combox = [[UIComboBox alloc] initWithFrame:CGRectMake(_sortedBtn1.right + kComboBoxLeftMargin, kComboBoxTopMargin, UISCREENWIDTH - 2 * kSegementedControlLeftMargin - kImageViewWidth - kSortedButtonWidth - kComboBoxLeftMargin - kFilterButtonWidth, kSelectedViewHeight - 2 * kComboBoxTopMargin)];
    _combox.entries = @[@"全部报价标的", @"已报价的标的"];
    _combox.selectedItem = 0;
    [_selectedView addSubview:_combox];
    
    _filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_filterBtn setTitle:@"筛选" forState:UIControlStateNormal];
    [_filterBtn setTitleColor:RGB_COLOR(@"#d91f00") forState:UIControlStateNormal];
    [_filterBtn addTarget:self action:@selector(filterBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _filterBtn.frame = CGRectMake(_combox.right, 0, kFilterButtonWidth, kSelectedViewHeight);
    [_selectedView addSubview:_filterBtn];
    
    [self.view addSubview:_selectedView];
}

- (void)customNoticeView{
    _noticeView = [[UIView alloc] initWithFrame:CGRectMake(0, _selectedView.bottom, UISCREENWIDTH, kSelectedViewHeight)];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(_selectedView.left + kSeparateLineLeftMargin, 0, UISCREENWIDTH - 2 * kSeparateLineLeftMargin, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [_noticeView addSubview:line];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(_selectedView.left + kSeparateLineLeftMargin, _noticeView.height - 1, UISCREENWIDTH - 2 * kSeparateLineLeftMargin, 1)];
    line2.backgroundColor = [UIColor lightGrayColor];
    [_noticeView addSubview:line2];
    
    NSMutableArray *blockArr = [NSMutableArray arrayWithCapacity:4];
    
    NSArray *array = @[@"已出价", @"未出价", @"价格变动", @"我的报价"];
    NSArray *colorArray = @[[UIColor redColor], [UIColor greenColor], [UIColor yellowColor], [UIColor blueColor]];
    
    for (int i = 0; i < 4; i++) {
        int width = (UISCREENWIDTH - 2 * kSeparateLineLeftMargin) / 4;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(width * i  + kSeparateLineLeftMargin, (_noticeView.height - kNoticeBlockHeight) / 2, kNoticeBlockHeight, kNoticeBlockHeight)];
        view.backgroundColor = colorArray[i];
        [blockArr addObject:view];
        [_noticeView addSubview:view];
    }
    
    for (int i = 0; i < 4; i++) {
        int width = (UISCREENWIDTH - 2 * kSeparateLineLeftMargin) / 4;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(width * i  + kSeparateLineLeftMargin + kNoticeBlockHeight, (_noticeView.height - kNoticeBlockHeight) / 2, 100, kNoticeBlockHeight)];
        label.text = array[i];
        CGRect rect = [label textRectForBounds:label.frame limitedToNumberOfLines:1];
        label.frame = CGRectMake(width * i  + kSeparateLineLeftMargin + kNoticeBlockHeight, (_noticeView.height - kNoticeBlockHeight) / 2, rect.size.width, rect.size.height);
        label.adjustsFontSizeToFitWidth = YES;
        [_noticeView addSubview:label];
    }
    
    [self.view addSubview:_noticeView];
    
    _noticeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kSegementedControlLeftMargin, _noticeView.bottom + kNoticeLabelTopMargin, _noticeView.width - 2 *kSegementedControlLeftMargin, kNoticeLabelHeight)];
    _noticeLabel.text = @"应价：报起始价格";
    _noticeLabel.textColor = RGB_COLOR(@"#555555");
    _noticeLabel.font = [UIFont systemFontOfSize:11];
    [self.view addSubview:_noticeLabel];
}

- (void)customTableView{
    _bigTableView = [[BigTableView alloc] initWithFrame:CGRectMake(0, _noticeLabel.bottom + kNoticeLabelBottomMargin, UISCREENWIDTH, UISCREENHEIGHT - _noticeLabel.bottom - self.tabBarController.tabBar.bounds.size.height) HeaderTitles:@[@"标的名称", @"所在期", @"剩余时间", @"当前价格", @"快速出价", @"竞价方式", @"报价次数", @"标的编号"] ColumnCount:8 RowHeadingCount:1 ColumnWidth:150 RowHeight:60];
    [self.view addSubview:_bigTableView];
}

- (void)customFilterView{
    _filterView1 = [[UIView alloc] initWithFrame:CGRectMake(0, _selectedView.bottom + 1, UISCREENWIDTH, _bigTableView.height + kNoticeLabelHeight + kSelectedViewHeight + self.tabBarController.tabBar.bounds.size.height)];
    _filterView1.backgroundColor = [UIColor whiteColor];
    
    UIComboBox *combox = [[UIComboBox alloc] initWithFrame:CGRectMake(30, 60, 200, 60)];
    combox.entries = @[@"big", @"small", @"too big"];
    combox.selectedItem = 2;
    [_filterView1 addSubview:combox];
}

#pragma mark - Actions

- (void)bbiClick:(UIBarButtonItem *)bbi{
    NSLog(@"消息");
}

- (void)changeView:(UISegmentedControl *)segment{
    NSLog(@"%ld", segment.selectedSegmentIndex);
    
    [_filterView1 removeFromSuperview];
    self.navigationController.tabBarController.tabBar.hidden = NO;
    _filterBtn.selected = YES;
    
    if (segment.selectedSegmentIndex == 0) {
        _combox.entries = @[@"全部报价标的", @"已报价的标的"];
        _combox.selectedItem = 0;
        _combox.frame = CGRectMake(_sortedBtn1.right + kComboBoxLeftMargin, kComboBoxTopMargin, UISCREENWIDTH - 2 * kSegementedControlLeftMargin - kImageViewWidth - kSortedButtonWidth - kComboBoxLeftMargin - kFilterButtonWidth, kSelectedViewHeight - 2 * kComboBoxTopMargin);
        [_sortedBtn2 removeFromSuperview];
    }else if (segment.selectedSegmentIndex == 1) {
        _combox.entries = @[@"进行中的标的", @"已结束的标的"];
        _combox.selectedItem = 0;
        _combox.frame = CGRectMake(_sortedBtn1.right + kComboBoxLeftMargin, kComboBoxTopMargin, UISCREENWIDTH - 2 * kSegementedControlLeftMargin - kImageViewWidth - kSortedButtonWidth - kComboBoxLeftMargin - kFilterButtonWidth, kSelectedViewHeight - 2 * kComboBoxTopMargin);
        [_sortedBtn2 removeFromSuperview];
    }else if(segment.selectedSegmentIndex == 2){
        _combox.entries = @[@"报价记录", @"成交结果"];
        _combox.selectedItem = 0;
        _combox.frame = CGRectMake(_sortedBtn1.right + _sortedBtn1.width + kComboBoxLeftMargin, kComboBoxTopMargin, UISCREENWIDTH - 2 * kSegementedControlLeftMargin - kImageViewWidth -  2 * kSortedButtonWidth - kComboBoxLeftMargin - kFilterButtonWidth, kSelectedViewHeight - 2 * kComboBoxTopMargin);
        [_selectedView addSubview:_sortedBtn2];
    }
    
    [_bigTableView.leftTableView reloadData];
    [_bigTableView.rightTableView reloadData];
}

- (void)filterBtnClick{
    if (_filterBtn.selected == YES) {
        if (_segmentedControl.selectedSegmentIndex != 2) {
            [self.view addSubview:_filterView1];
            self.navigationController.tabBarController.tabBar.hidden = YES;
        }else{
            [self.view addSubview:_filterView1];
        }
    }else{
        [_filterView1 removeFromSuperview];
        self.navigationController.tabBarController.tabBar.hidden = NO;
    }
    _filterBtn.selected = !_filterBtn.selected;
}

@end
